//
//  DDAPIGenerate.m
//  DingDongZP
//
//  Created by Jyh on 16/3/18.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDAPIGenerate.h"

//const NSString* defaultNetworkHost =  @"www.51ledian.com";
<<<<<<< HEAD
const NSString* defaultNetworkHost =  @"114.55.59.142";
const NSString* defaultNetworkHostTest =@"";
const NSString* defaultDevelopeServerHost = @"";
=======
const NSString* appNetworkHostDefault =  @"www.dingdong.cc"; //正式环境
const NSString* appNetworkHostTest =     @"test.dingdong.cc"; //测试环境
>>>>>>> dingdong2.1.0

static NSString* const apiFileName = @"DDAPIConfig";
static NSString* const apiFileExtension = @"json";

@implementation DDAPIGenerate
{
    NSDictionary *cachedDictionary;
}
+(NSDictionary*)apiDictionary
{
    NSString* filePath = [[NSBundle mainBundle] pathForResource:apiFileName ofType:apiFileExtension];
    NSData* data = [NSData dataWithContentsOfFile:filePath];
    NSError *err = nil;
    NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&err];
    if (err) {
<<<<<<< HEAD
        NSLog(@"--------------- API Json文件有问题！---------------");
=======
//        NSLog(@"--------------- API Json文件有问题！---------------");
>>>>>>> dingdong2.1.0
    }
    return dic;
}

+ (DDAPIGenerate *)sharedInstance
{
    static DDAPIGenerate* sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(& onceToken,^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (NSString *)defaultProtocol {
    if (!_defaultProtocol) {
<<<<<<< HEAD
        return @"http";
=======
        return @"https";
>>>>>>> dingdong2.1.0
    }
    return _defaultProtocol;
}

-(NSString*)API:(NSString*)apiName
{
    if (!apiName) {
        return nil;
    }
    NSDictionary* d;
    if (!cachedDictionary) {
        d=[[self class] apiDictionary];
        if (d && d.count > 0) {
            cachedDictionary = d;
        }
    }
    else
    {
        d = cachedDictionary;
    }
    
    if (!d) {
        return nil;
    }
    
    NSDictionary * dic=[d objectForKey:apiName];
    NSString *apiProtocol = @"https";
    
    if (_serverType == kLinkServerTypeOfNormal) {
<<<<<<< HEAD
        apiProtocol=[dic objectForKey:@"protocol"]?[dic objectForKey:@"protocol"]:@"http";
    } else if (_serverType == kLinkServerTypeOfDeveloper) {
        apiProtocol = @"http";
=======
        apiProtocol=[dic objectForKey:@"protocol"]?[dic objectForKey:@"protocol"]:@"https";
    } else if (_serverType == kLinkServerTypeOfTest) {
        apiProtocol = @"https";
>>>>>>> dingdong2.1.0
    }
    
    const NSString* host = [self serverHostWithType:self.serverType];
    NSString* control = nil, *action = nil;

<<<<<<< HEAD
    control  =  [dic objectForKey:@"control"] ? [dic objectForKey:@"control"] : @" ";
=======
    
    
    control  =  [self getControlType:self.serverType andInfoDict:dic];
>>>>>>> dingdong2.1.0
    action   =  [dic objectForKey:@"action"] ? [dic objectForKey:@"action"] : @" ";

    NSString* apiUrl = [NSString stringWithFormat:@"%@://%@/%@/%@.php",apiProtocol,host,control,action];
    
    return apiUrl;
}

<<<<<<< HEAD
- (const NSString *)serverHostWithType:(kLinkServerType)type {
    switch (type) {
        case kLinkServerTypeOfNormal: {
            return defaultNetworkHost;
        }
            break;
        case kLinkServerTypeOfDeveloper: {
            return defaultDevelopeServerHost;
        }
            break;
        case kLinkServerTypeOfTest: {
            return defaultNetworkHostTest;
        }
            break;
=======
- (NSString *)getControlType: (kLinkServerType) type andInfoDict:(NSDictionary *)dic{
    
    if (type == kLinkServerTypeOfNormal) {
        
        return [dic objectForKey:@"control"] ? [dic objectForKey:@"control"] : @"";
    } else {
        
//        return @"appwstest";
        return @"appws";
    }
    
}

- (const NSString *)serverHostWithType:(kLinkServerType)type {
    switch (type) {
        case kLinkServerTypeOfNormal: {
            
            return appNetworkHostDefault;
            break;
        }
    
        case kLinkServerTypeOfTest: {
            
            return appNetworkHostTest;
            break;
        }
        
//        case kLinkServerTypeOfTest: {
//            return defaultNetworkHostTest;
//        }
//            break;
>>>>>>> dingdong2.1.0
            
        default:
            break;
    }
}

+ (NSString *)imageURLWithPath:(NSString *)path
{
<<<<<<< HEAD
    if (path) {
        if (![path hasPrefix:@"logo/"]) {
            path = [path stringByReplacingCharactersInRange:NSMakeRange(0, 0) withString:@"logo/"];
        }
        return [NSString stringWithFormat:@"http://%@/appws/%@", defaultNetworkHost, path];
    }
    return nil;
=======
    
    NSString *urlstr = [[NSString alloc] init];
    
    if (path) {
        
        if (![path hasPrefix:@"logo/"]) {
            path = [path stringByReplacingCharactersInRange:NSMakeRange(0, 0) withString:@"logo/"];
        }
        
        //获取服务器前缀

        if ([DDAPIGenerate sharedInstance].serverType == kLinkServerTypeOfNormal) {
         
            urlstr = [NSString stringWithFormat:@"https://%@/appws/%@", appNetworkHostDefault, path];
        } else{
            
            urlstr = [NSString stringWithFormat:@"https://%@/appws/%@", appNetworkHostTest, path];
        }
        
    }
    return urlstr;
>>>>>>> dingdong2.1.0
}

+ (NSString *)h5URLWithPath:(NSString *)path
{
    if (path) {
<<<<<<< HEAD
        return [NSString stringWithFormat:@"http://%@/appws/%@", defaultNetworkHost, path];
    }
    return nil;
}

=======
        
        if ([DDAPIGenerate sharedInstance].serverType == kLinkServerTypeOfNormal) {
            
            return [NSString stringWithFormat:@"https://%@/appws/%@", appNetworkHostDefault, path];
        } else {
            
            return [NSString stringWithFormat:@"https://%@/appws/%@", appNetworkHostTest, path];
        }
    }
    
    return nil;
}

/**
 *   钱包功能以后的接口   API要更换
 */
-(NSString*)NewAPI:(NSString*)newApiName
{
    if (!newApiName) {
        return nil;
    }
    NSDictionary* d;
    if (!cachedDictionary) {
        d=[[self class] apiDictionary];
        if (d && d.count > 0) {
            cachedDictionary = d;
        }
    }
    else
    {
        d = cachedDictionary;
    }
    
    if (!d) {
        return nil;
    }
    
    NSDictionary * dic=[d objectForKey:newApiName];
    NSString *apiProtocol = @"https";
    
    if (_serverType == kLinkServerTypeOfNormal) {
        
        apiProtocol=[dic objectForKey:@"protocol"]?[dic objectForKey:@"protocol"]:@"https";
    } else if (_serverType == kLinkServerTypeOfTest) {
        
        apiProtocol = @"https";
    }
    
    const NSString* host = [self serverHostWithType:self.serverType];
    NSString* control = nil, *action = nil;
    
    
    
    control  =  [self getControlType:self.serverType andInfoDict:dic];
    action   =  [dic objectForKey:@"action"] ? [dic objectForKey:@"action"] : @" ";
    
    NSString* apiUrl = [NSString stringWithFormat:@"%@://%@/%@/%@",apiProtocol,host,control,action];
    
    return apiUrl;
}

>>>>>>> dingdong2.1.0
@end
